
/**
 * _Utils.java
 * @author Jason Lu
 * @date 2014-1-2
 * @version 1.0
 */
package com.zhike.common.utils;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Jason
 */
@Slf4j
public class _Utils {

	private static List<String> provinceList = null;

	static {
		provinceList = new ArrayList<String>();
		provinceList.add("北京");
		provinceList.add("天津");
		provinceList.add("上海");
		provinceList.add("重庆");
		provinceList.add("河南");
		provinceList.add("河北");
		provinceList.add("云南");
		provinceList.add("辽宁");
		provinceList.add("黑龙");
		provinceList.add("湖南");
		provinceList.add("安徽");
		provinceList.add("山东");
		provinceList.add("新疆");
		provinceList.add("江苏");
		provinceList.add("江西");
		provinceList.add("浙江");
		provinceList.add("山西");
		provinceList.add("湖北");
		provinceList.add("广西");
		provinceList.add("甘肃");
		provinceList.add("山西");
		provinceList.add("内蒙");
		provinceList.add("陕西");
		provinceList.add("吉林");
		provinceList.add("福建");
		provinceList.add("贵州");
		provinceList.add("广东");
		provinceList.add("青海");
		provinceList.add("西藏");
		provinceList.add("四川");
		provinceList.add("宁夏");
		provinceList.add("海南");
		provinceList.add("台湾");
		provinceList.add("香港");
		provinceList.add("澳门");
	}

	// -----------------------------------IPSeeker
	private static StringBuilder sb = new StringBuilder();

	/**
	 * 从ip的字符串形式得到字节数组形式
	 * 
	 * @param ip
	 *           字符串形式的ip
	 * @return 字节数组形式的ip
	 */
	public static byte[] getIpByteArrayFromString(String ip) {
		byte[] ret = new byte[4];
		StringTokenizer st = new StringTokenizer(ip, ".");
		try {
			ret[0] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
			ret[1] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
			ret[2] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
			ret[3] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
		} catch (Exception e) {
			log.warn("从ip的字符串形式得到字节数组形式报错");
		}
		return ret;
	}

	public static void main(String[] args) {
		System.out.println(provinceList.size());
	}

	/**
	 * @param ip
	 *           ip的字节数组形式
	 * @return 字符串形式的ip
	 */
	public static String getIpStringFromBytes(byte[] ip) {
		try {
			sb.delete(0, sb.length());
			sb.append(ip[0] & 0xFF);
			sb.append('.');
			sb.append(ip[1] & 0xFF);
			sb.append('.');
			sb.append(ip[2] & 0xFF);
			sb.append('.');
			sb.append(ip[3] & 0xFF);
			return sb.toString();
		} catch (Exception e) {
			log.warn("从ip的byte[]形式得到字符串形式报错：" + e.getMessage());
		}

		return "";
	}

	/**
	 * 根据某种编码方式将字节数组转换成字符串
	 * 
	 * @param b
	 *                 字节数组
	 * @param offset
	 *                 要转换的起始位置
	 * @param len
	 *                 要转换的长度
	 * @param encoding
	 *                 编码方式
	 * @return 如果encoding不支持，返回一个缺省编码的字符串
	 */
	public static String getString(byte[] b, int offset, int len, String encoding) {
		try {
			return new String(b, offset, len, encoding);
		} catch (UnsupportedEncodingException e) {
			return new String(b, offset, len);
		}
	}

	public static String checkChinaArea(String area) {
		if (area != null && provinceList.contains(area.length() > 2 ? area.substring(0, 2) : area))
			return "中国";
		return area;
	}

	public static String formatDate(Object date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static String getDayTime(int num, String pattern) {
		GregorianCalendar gregorianCal = new GregorianCalendar();
		gregorianCal.set(Calendar.DATE, gregorianCal.get(Calendar.DATE) + num);
		return formatDate(gregorianCal.getTime(), pattern);
	}

	public static boolean isPhoneNumber(String input) {
		// 定义手机号的正则表达式
		String regex = "^1[3-9]\\d{9}$";
		// 创建 Pattern 对象
		Pattern pattern = Pattern.compile(regex);
		// 创建 Matcher 对象
		Matcher matcher = pattern.matcher(input);
		// 进行匹配
		return matcher.matches();
	}
}
